import Vue from 'vue'
import Router from 'vue-router'
import store from '../store'

function  hasUrl(url){
  return store.getters.getUserInfo.menus_url.some(item=>item==url)
}


Vue.use(Router)

export   const indexRoutes = [  {
  path: '/menu',
  component: () => import('@/views/menu/menu'),
  name: '菜单管理',
 beforeEnter(to,from,next){
    hasUrl('/menu')?next():next('/home')
  }
},
{
  path: '/roler',
  component: () => import('@/views/roler/roler'),
  name: '角色管理',
 beforeEnter(to,from,next){
    hasUrl('/roler')?next():next('/home')
  }
},
{
  path: '/manger',
  component: () => import('@/views/manger/manger'),
  name: '管理员管理',
 beforeEnter(to,from,next){
    hasUrl('/manger')?next():next('/home')
  }
},
{
  path: '/goodscate',
  component: () => import('@/views/goodscate/goodscate'),
  name: '商品分类管理'
},
{
  path: '/goodsspecs',
  component: () => import('@/views/goodsspecs/goodsspecs'),
  name: '商品规格管理'
},
{
  path: '/goods',
  component: () => import('@/views/goods/goods'),
  name: '商品管理'
},
{
  path: "/banner",
  component: () => import("@/views/banner/banner"),
  name: "轮播图管理",
},
{
  path: "/member",
  component: () => import("@/views/member/member"),
  name: "会员管理",

},
{
  path: "/seck",
  component: () => import("@/views/seck/seck"),
  name: "秒杀管理",

},]
let router = new Router({
  routes: [{
      path: '/login',
      component: () => import('@/pages/login')
    },
    {
      path: '/index',
      component: () => import('@/pages/index'),
      children: [{
          path: '/home',
          component: () => import('@/views/home'),
          name: '首页'
        },
      ...indexRoutes,
        {
          path: '',
          redirect: '/home'
        }
      ]
    },
    {
      path: '*',
      redirect: '/login'
    }
  ]
})
router.beforeEach((to, from, next) => {
  if (to.path == "/login") {
    next();
    return;
  }

  let userInfo = sessionStorage.getItem('userInfo') ? JSON.parse(sessionStorage.getItem('userInfo')) : {}
  // console.log(store.getters, "store.gettersstore.getters");
  if (userInfo) {
    next();
    return;
  }
  next("/login");
});
export default router
